/*
 * Created on 08.03.2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.higginsexamples.multiusermail.rcpmail;

import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.auth.AuthScheme;
import org.apache.commons.httpclient.auth.AuthenticationException;
import org.apache.commons.httpclient.auth.MalformedChallengeException;
import org.pf.six.Element;
import org.pf.six.XmlStreamWriter;
import org.pf.text.StringUtil;
import org.pf.util.Base64Converter;

public class BasicSecurityAuthScheme implements AuthScheme
{
	protected static final StringUtil SU = StringUtil.current();
	
	private String[] requiredClaims ;
	private String[] optionalClaims ;

	public String[] getOptionalClaims()
	{
		return optionalClaims;
	}

	public void setOptionalClaims( String[] optionalClaims )
	{
		this.optionalClaims = optionalClaims;
	}

	public String[] getRequiredClaims()
	{
		return requiredClaims;
	}

	public void setRequiredClaims( String[] requiredClaims )
	{
		this.requiredClaims = requiredClaims;
	}

	
	public String authenticate( Credentials credentials, HttpMethod method ) throws AuthenticationException
	{
		XmlsoapAttributesCredentials creds;
		Element root ;
		Element attribsTag ;
		Element attrTag ;
		String[] attrNames ;
		XmlStreamWriter streamWriter ;
		Writer writer ;
		String authToken ;

		System.out.println( "authenticate 1" );
		
		if ( credentials instanceof XmlsoapAttributesCredentials )
		{
			creds = (XmlsoapAttributesCredentials) credentials;
			root = new Element( SecurityConstants.TAG_TOKEN );
			attribsTag = new Element( SecurityConstants.TAG_ATTRIBUTES );
			root.addChild( attribsTag ) ;
			attrNames = creds.getAttrNames() ;
			for (int i = 0; i < attrNames.length; i++ )
			{
				attrTag = new Element( SecurityConstants.TAG_ATTRIBUTE, creds.getAttribute( attrNames[i] ) ) ;
				attrTag.setAttribute( SecurityConstants.ATTR_NAME, SecurityConstants.ATTRIBUTE_MAPPING.get(attrNames[i]) ) ;
				attribsTag.addChild( attrTag ) ;
			}
			writer = new StringWriter(300) ;
			streamWriter = new XmlStreamWriter() ;
			streamWriter.appendDocumentStart( writer, SecurityConstants.XML_ENCODING ) ;
			streamWriter.appendSIXElement( root, writer ) ;
			authToken = writer.toString();
			System.out.println( "Token: " + authToken );
			return Base64Converter.encode( authToken );
		}
		return null;
	}

	public String authenticate( Credentials credentials, String method, String uri ) throws AuthenticationException
	{
		System.out.println( "authenticate 2" );
		return null;
	}

	public String getID()
	{
		return SecurityConstants.AUTH_TYPE;
	}

	public String getParameter( String name )
	{
		System.out.println( "getParameter" );
		// TODO Auto-generated method stub
		return null;
	}

	public String getRealm()
	{
		// TODO Auto-generated method stub
		return null;
	}

	public String getSchemeName()
	{
		return SecurityConstants.AUTH_TYPE;
	}

	public boolean isComplete()
	{
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isConnectionBased()
	{
		return false;
	}

	public void processChallenge( String challenge ) throws MalformedChallengeException
	{
		String parameters ;
		Map params ;
		
		System.out.println( "Challenge: " + challenge );
		
		parameters = SU.cutHead( challenge, " " ) ;
		params = SU.asMap( parameters );

		parameters = (String) params.get( SecurityConstants.REQUIRED ) ;
		parameters = Base64Converter.decodeToString( parameters ) ;
		this.setRequiredClaims( SU.parts( parameters, SecurityConstants.CLAIM_SEPARATOR ) ) ;

		parameters = (String) params.get( SecurityConstants.OPTIONAL ) ;
		parameters = Base64Converter.decodeToString( parameters ) ;
		this.setOptionalClaims( SU.parts( parameters, SecurityConstants.CLAIM_SEPARATOR ) ) ;		
	}

}
